package com.swind.fast.modules.app.space.rent.service.impl;

import com.swind.fast.modules.app.space.rent.entity.SpaceContract;
import com.swind.fast.modules.app.space.rent.mapper.SpaceContractMapper;
import com.swind.fast.modules.app.space.rent.service.ISpaceContractService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swind.fast.modules.app.space.rent.event.SpaceContractCreateEvent;
import com.swind.fast.modules.app.space.rent.event.SpaceSyncAnnounceEvent;

import com.swind.fast.modules.app.space.rent.entity.SpaceSite;
import com.swind.fast.modules.app.space.rent.service.ISpaceSiteService;
import com.swind.fast.modules.app.space.rent.entity.SpaceRoom;
import com.swind.fast.modules.app.space.rent.service.ISpaceRoomService;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import org.apache.commons.lang3.StringUtils;
import com.swind.fast.common.util.oConvertUtils;

import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.List;
import java.util.HashMap;

/**
 * @Description: 项目
 * @Author: swind
 * @Date:   2021-11-20
 * @Version: V1.0
 */
@Slf4j
@Service
public class SpaceContractServiceImpl extends ServiceImpl<SpaceContractMapper, SpaceContract> implements ISpaceContractService {

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private ISpaceSiteService spaceSiteService;

    @Autowired
    private ISpaceRoomService spaceRoomService;

    public String getStatisticData() {
        DateTime cdate = new DateTime();
        String cmonth = cdate.toDateStr();
        String emonth = cdate.offset(DateField.MONTH,1).toDateStr();
        String nemonth = cdate.offset(DateField.MONTH,1).toDateStr();

        QueryWrapper<SpaceContract> queryWrapper = new QueryWrapper<SpaceContract>().select("count(id) as total").gt("end_date", cmonth).lt("end_date", emonth);
        Map<String, Object> obj = this.getMap(queryWrapper);

        queryWrapper = new QueryWrapper<SpaceContract>().select("count(id) as total").gt("end_date", cmonth).lt("end_date", nemonth);
        Map<String, Object> obj2 = this.getMap(queryWrapper);

        log.info("cmonth: {}, emonth: {}, nemonth: {}, obj: {}, obj2: {}", cmonth, emonth, nemonth, obj, obj2);

        queryWrapper = new QueryWrapper<SpaceContract>().select("sum(deposit) as total").eq("retreat", "N");
        Map<String, Object> obj3 = this.getMap(queryWrapper);
        if (oConvertUtils.isEmpty(obj3)) {
            obj3 = new HashMap<String, Object>();
            obj3.put("total", 0);
        }

        log.info("obj3: {}", obj3);

        return obj.get("total").toString() + "," + obj2.get("total").toString() + "," + obj3.get("total").toString();
    }

    public String getExpiredData() {
        DateTime cdate = new DateTime();
        String cmonth = cdate.toDateStr();
        String emonth = cdate.offset(DateField.MONTH,1).toDateStr();
        String nemonth = cdate.offset(DateField.MONTH,1).toDateStr();

        QueryWrapper<SpaceContract> queryWrapper = new QueryWrapper<SpaceContract>().select("count(id) as total").gt("end_date", cmonth).lt("end_date", emonth);
        Map<String, Object> obj = this.getMap(queryWrapper);

        queryWrapper = new QueryWrapper<SpaceContract>().select("count(id) as total").gt("end_date", cmonth).lt("end_date", nemonth);
        Map<String, Object> obj2 = this.getMap(queryWrapper);

        // log.info("cmonth: {}, emonth: {}, nemonth: {}, obj: {}, obj2: {}", cmonth, emonth, nemonth, obj, obj2);
        return obj.get("total").toString() + "," + obj2.get("total").toString();
    }

    public void saveMain(SpaceContract spaceContract) {
        this.save(spaceContract);

        applicationContext.publishEvent(new SpaceContractCreateEvent(spaceContract));
        applicationContext.publishEvent(new SpaceSyncAnnounceEvent(""));
    }

    public String genNumber(String siteId, String roomId) {
        String contractNumber = "SQ";
        SpaceSite site = spaceSiteService.getById(siteId);
        SpaceRoom room = spaceRoomService.getById(roomId);

        if (site == null || room == null)
            return "";

        String siteName = site.getTitle();
        String siteNumber = "";
        if (siteName.indexOf("华声") > 0) {
            siteNumber = "HS";
        }
        if (siteName.indexOf("朝阳") > 0) {
            siteNumber = "CY";
        }
        if (siteName.indexOf("国贸") > 0) {
            siteNumber = "GM";
        }
        String year = new DateTime().toString("YYYY");
        String roomTitle = room.getRoom();
        contractNumber = contractNumber + year + siteNumber + roomTitle;

        QueryWrapper<SpaceContract> queryWrapper = new QueryWrapper<SpaceContract>().select("id").likeRight("number", contractNumber);
        List<SpaceContract> list = this.list(queryWrapper);
        String size = String.valueOf(list.size() + 1);
        size = StringUtils.leftPad(size, 2, "0");
        contractNumber = contractNumber + "-" + size;

        return contractNumber;
    }

}
